/**
 * Created with IntelliJ IDEA.
 * Description:
 * User: 刘远望
 * Date: 2025-04-13
 * Time: 22:11
 */
public class MyCircleQueue {
    public int front;
    public int rear;
    public int[] elem;

    public MyCircleQueue(int k) {
        elem = new int[k + 1];
    }

    public boolean enQueue(int val){
        if(isFull()){
            return false;
        }else {
            elem[rear] = val;
            rear = (rear + 1) % elem.length;
            return true;
        }
    }

    public boolean deQueue(){
        if(isEmpty()){
            return false;
        }else {
            front = (front + 1) % elem.length;
            return true;
        }
    }
    //获得队列的头元素
    public int Front(){
        if(isEmpty()){
            return -1;
        }else{
            return elem[front];
        }
    }
    //获得队尾元素
    public int Rear(){
        if(isEmpty()){
            return -1;
        }else{
            int index = (rear == 0) ? elem.length - 1 : rear - 1;
            return elem[index];
        }
    }

    public boolean isEmpty(){
        return front == rear;
    }

    public boolean isFull(){
        return front == (rear + 1) % elem.length;
    }
}
